#include "kernel-c/kernel.h"
#include "kernel-cpp/kernel-class.h"

// 获取时间

int64 GetCurrentTime() {
    int64 time{};
    KS_getClock(&time, KS_CLOCK_MEASURE_HIGHEST);
    return time;
}



KSError _declspec(dllexport) __stdcall GetNumber(int& number) {
    KernelClass kernelClass;
    std::shared_ptr<KernelClass> kernelClassPtr = std::make_shared<KernelClass>();
    kernelClassPtr->setNumber(654321);
    kernelClass.setNumber(654321);
    number = kernelClass.getNumber();
    KS_string myString("Hello, KS_allocator!");
    myString.append("9999");
    myString += "888888";
    KS_string myString2 = myString.substr(0, 10);
    auto iterator  = std::find(myString.begin(), myString.end(), '9');
    if (iterator != myString.end()) {
        KS_printK("find 9\n");
    }
    KS_printK("int start\n");
    auto start_time = GetCurrentTime();
    for (int i = 0; i < 100000; i++) {
        int t{};
    }
    auto end_time = GetCurrentTime();
    int64 machine_time = end_time - start_time;
    KS_convertClock(&machine_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_convertClock(&start_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_convertClock(&end_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_printK("int end, time = %d  * 100ns, start_time = %d, end_time = %d",
              machine_time, start_time, end_time);

    KS_printK("KS_vector start\n");
    start_time = GetCurrentTime();
    for (int i = 0; i < 100000; i++) {
        KS_vector<int> myVector;
    }
    end_time = GetCurrentTime();
    machine_time = end_time - start_time;
    KS_convertClock(&machine_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_convertClock(&start_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_convertClock(&end_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_printK("KS_vector end, time = %d  * 100ns, start_time = %d, end_time = %d",
              machine_time, start_time, end_time);

    KS_printK("KS_vector start push\n");
    start_time = GetCurrentTime();
    KS_vector<int> myVector;
    for (int i = 0; i < 1000000; i++) {
        myVector.push_back(i);
    }
    end_time = GetCurrentTime();
    machine_time = end_time - start_time;
    KS_convertClock(&machine_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_convertClock(&start_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_convertClock(&end_time, KS_CLOCK_MEASURE_HIGHEST,
                    KS_CLOCK_MACHINE_TIME, KSF_NO_FLAGS);
    KS_printK("KS_vector end push, time = %d  * 100ns, start_time = %d, end_time = %d",
              machine_time, start_time, end_time);

    KS_printK("KS_map start\n");
    KS_map<int, KS_string> myMap;
    myMap.insert(std::pair<int, KS_string>(1, "hello"));
    myMap.insert(std::pair<int, KS_string>(2, "world"));
    KS_printK("KS_map end\n");

    KS_printK("KS_unordered_map start\n");
    KS_unordered_map<int, KS_string> myUnorderedMap;
    myUnorderedMap.insert(std::pair<int, KS_string>(1, "hello"));
    myUnorderedMap.insert(std::pair<int, KS_string>(2, "world"));
    KS_printK("KS_unordered_map end\n");

    KS_printK("KS_set start\n");
    KS_set<int> mySet;
    mySet.insert(1);
    mySet.insert(2);
    KS_printK("KS_set end\n");

    KS_printK("KS_list start\n");
    KS_list<int> myList;
    myList.push_back(1);
    myList.push_back(2);
    KS_printK("KS_list end\n");

    KS_printK("string = %s\n", myString.data());
    KS_printK("string2 = %s\n", myString2.data());
    KS_printK("stringtest = %s\n", "999999999999999");
    KS_printK("vector[0] = %d\n", myVector[0]);
    KS_printK("map[1] = %s\n", myMap[1].data());
    KS_printK("unordered_map[1] = %s\n", myUnorderedMap[1].data());
    KS_printK("set[0] = %d\n", *mySet.begin());
    KS_printK("list[0] = %d\n", myList.front());

    return KS_OK;
}

KSError _declspec(dllexport) __stdcall _exitClassFunction(void* argc, void* content) {
    KS_string myString = "1111111111111111111";
    KS_printK("string = %s\n", myString.data());
    return KS_OK;
}

#define WIN32_LEAN_AND_MEAN
#pragma pack(push, 8)
#include <windows.h>
#pragma pack(pop)

BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD reason, LPVOID pReserved) {
  return TRUE;
}